// 测试订单API的脚本
const https = require('https');

// 测试微信小店API调用
async function testWechatOrderAPI() {
  // 这里需要替换为实际的access_token
  const accessToken = 'YOUR_ACCESS_TOKEN';
  const openid = 'YOUR_OPENID';
  
  const requestData = {
    openid: openid,
    page_size: 50,
    create_time_range: {
      start_time: Math.floor((Date.now() - 30 * 24 * 60 * 60 * 1000) / 1000),
      end_time: Math.floor(Date.now() / 1000)
    }
  };

  console.log('请求参数:', JSON.stringify(requestData, null, 2));
  
  const url = `https://api.weixin.qq.com/channels/ec/order/list/get?access_token=${accessToken}`;
  
  const postData = JSON.stringify(requestData);
  
  const options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Content-Length': Buffer.byteLength(postData)
    }
  };

  return new Promise((resolve, reject) => {
    const req = https.request(url, options, (res) => {
      let data = '';
      
      res.on('data', (chunk) => {
        data += chunk;
      });
      
      res.on('end', () => {
        try {
          const response = JSON.parse(data);
          console.log('API响应:', JSON.stringify(response, null, 2));
          resolve(response);
        } catch (error) {
          reject(error);
        }
      });
    });

    req.on('error', (error) => {
      reject(error);
    });

    req.write(postData);
    req.end();
  });
}

// 如果直接运行此脚本
if (require.main === module) {
  console.log('这是一个测试脚本，需要在实际环境中替换access_token和openid');
  console.log('请在小程序中测试修改后的云函数');
}

module.exports = { testWechatOrderAPI };
